package com.chris.homecare.components
{
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.data.DataUtil;
	import com.chris.homecare.data.FetchRemoteData;
	
	import flash.geom.Rectangle;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.TextInput;
	import feathers.display.Scale9Image;
	import feathers.events.FeathersEventType;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.VerticalLayout;
	import feathers.textures.Scale9Textures;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	public class NewsLetterPopup extends LayoutGroup
	{
		private var backgroundImage:Scale9Image;
		private var layoutGroup:LayoutGroup;

		private var emailTxtBox:TextInput;
		private var errorTextLabel:Label;
		
		public function NewsLetterPopup()
		{
			super();
		}
		
		override protected function initialize():void
		{
			const texture:Scale9Textures = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("popupBackground"), new Rectangle(20,20,60,60));
			backgroundImage = new Scale9Image(texture);
			addChild(backgroundImage);
			
			layoutGroup = new LayoutGroup();
			var layout:VerticalLayout = new VerticalLayout();
			layout.gap = 8;
			layout.padding = 10;
			layout.horizontalAlign = VerticalLayout.HORIZONTAL_ALIGN_CENTER;
			layoutGroup.layout = layout;
			addChild(layoutGroup);
			
			var newsLetterLabel:Label = new Label();
			newsLetterLabel.nameList.add("news-letter-label");
			newsLetterLabel.text = "Would you like to subscribe for News Letter emails?";
			layoutGroup.addChild(newsLetterLabel);
			
			var gapginQuad:Quad = new Quad(20,5,0xFFFFFF);
			gapginQuad.alpha = 0;
			layoutGroup.addChild(gapginQuad);
			
			emailTxtBox = new TextInput();
			emailTxtBox.addEventListener( FeathersEventType.FOCUS_IN, onInputFocusIn);
			emailTxtBox.prompt = "please enter your email";
			layoutGroup.addChild(emailTxtBox);
			
			errorTextLabel = new Label();
			errorTextLabel.text = "Please enter valid email";
			errorTextLabel.nameList.add("error-label");
			errorTextLabel.visible = false;
			layoutGroup.addChild(errorTextLabel);
			
			var horLayoutGroup:LayoutGroup = new LayoutGroup();
			var horLayout:HorizontalLayout = new HorizontalLayout();
			horLayout.gap = 20;
			horLayout.paddingTop = 10;
			horLayoutGroup.layout = horLayout;
			layoutGroup.addChild(horLayoutGroup);
			
			var yesBtn:Button = new Button();
			yesBtn.label = "Yes";
			yesBtn.nameList.add("plain-btn");
			yesBtn.addEventListener(Event.TRIGGERED, onYesBtnTriggered);
			horLayoutGroup.addChild(yesBtn);
			
			var noBtn:Button = new Button();
			noBtn.label = "No, thanks";
			noBtn.addEventListener(Event.TRIGGERED, onNoBtnTriggered);
			noBtn.nameList.add("plain-btn");
			horLayoutGroup.addChild(noBtn);
			
			super.initialize();
		}
		
		private function onYesBtnTriggered():void
		{
			if(isValidUserEmail(emailTxtBox.text))
			{
				FetchRemoteData.getClient().postEmailData(emailTxtBox.text);
				this.dispatchEventWith(Event.COMPLETE);
			}
			
			else 
			{
				errorTextLabel.visible = true;
			}
		}
		
		private function onNoBtnTriggered():void
		{
			DataUtil.updateSettings();
			this.dispatchEventWith(Event.COMPLETE);
		}
		
		private function onInputFocusIn():void
		{
			errorTextLabel.visible = false;
		}
		
		override protected function draw():void
		{
			super.draw();
			backgroundImage.width = this.width;
			backgroundImage.height = this.height;
			
			errorTextLabel.width = emailTxtBox.width = this.width - 40;
		}
		
		private function isValidUserEmail(email:String):Boolean 
		{
			var myRegExp:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
			var myResult:Object = myRegExp.exec(email);
			if (myResult == null) 
			{
				return false;
			}
			return true;
		}
		
	}
}